package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAlluser() {
        List<User> userList = new ArrayList<>();
        Connection con = JDBCUtils.getConnection();
        Statement stat = null;
        ResultSet rs = null;
        try {
            stat = con.createStatement();
            String sql = "select * from user";
            rs = stat.executeQuery(sql);

            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                String qq = rs.getString("qq");
                String email = rs.getString("email");
                String username = rs.getString("username");
                String password = rs.getString("PASSWORD");
                User user = new User(id,name,gender,age,address,qq,email,username,password);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,stat,rs);
        }
        return userList;
    }


    @Override
    public void addUser(User user) {
        Connection con = JDBCUtils.getConnection();
        Statement stat = null;
        try {
            stat = con.createStatement();
            String sql = "insert into user values(null,'" + user.getName() + "','" + user.getGender() + "'," + user.getAge() + ",'" + user.getAddress() + "','" + user.getQq() + "','" + user.getEmail() + "',null,null)";
            stat.executeUpdate(sql);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,stat);
        }
    }

    @Override
    public User findOneUser(String uid) {
        Connection con = JDBCUtils.getConnection();
        Statement stat = null;
        ResultSet rs = null;
        User user = null;
        try {
            stat = con.createStatement();
            String sql = "select * from user where id="+uid;
            rs = stat.executeQuery(sql);

            if (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                String qq = rs.getString("qq");
                String email = rs.getString("email");
                String username = rs.getString("username");
                String password = rs.getString("PASSWORD");
                user = new User(id,name,gender,age,address,qq,email,username,password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,stat,rs);
        }

        return user;
    }

    @Override
    public void updateUser(User user) {
        Connection con = JDBCUtils.getConnection();
        Statement stat = null;
        try {
            stat = con.createStatement();
            String sql = "update user set name = '" + user.getName() + "',gender = '" + user.getGender() + "' ,age = " + user.getAge() + " , address = '" + user.getAddress() + "' , qq = '" + user.getQq() + "', email = '" + user.getEmail() + "' where id = " + user.getId();
            stat.executeUpdate(sql);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,stat);
        }
    }

    @Override
    public void deleteUser(String id) {
        Connection con = JDBCUtils.getConnection();
        Statement stat = null;
        try {
            stat = con.createStatement();
            String sql = "delete from user where id = " + id;
            stat.executeUpdate(sql);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,stat);
        }
    }

    @Override
    public boolean login(User use) {
        Connection con = JDBCUtils.getConnection();
        Statement stat = null;
        ResultSet rs = null;
        User user = null;
        try {
            stat = con.createStatement();
            String sql = "select * from user where username='"+use.getUsername() +"'and password='"+use.getPassword()+"'";
            rs = stat.executeQuery(sql);

            if (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                String qq = rs.getString("qq");
                String email = rs.getString("email");
                String username = rs.getString("username");
                String password = rs.getString("PASSWORD");
                user = new User(id,name,gender,age,address,qq,email,username,password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(con,stat,rs);
        }

        if (user == null) {
            return false;
        }else {
            return true;
        }
    }
}
